<template>
  <div class="login-container">
    <!-- 头部 -->
    <van-nav-bar title="登录" left-arrow>
      <template #left>
        <img class="logo" src="@/assets/image/logo.png" />
      </template>
    </van-nav-bar>

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="fromLogin.mobile"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="rules.tel"
      />
      <van-field
        v-model="fromLogin.code"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="rules.password"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          :loading="isloading"
          type="info"
          loading-text="加载中..."
          native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// import { longinAPI } from "@/api/login";
import { mapActions } from "vuex";

import { Notify } from "vant";
export default {
  data() {
    return {
      isloading: false,
      fromLogin: {
        mobile: "13888888888",
        code: "246810",
      },
      // 校验规则
      rules: {
        tel: [
          {
            required: true,
            message: "请输入手机号!",
            trigger: "onChange",
          },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "onChange",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入验证码!",
            trigger: "onChange",
          },
          {
            pattern: /^\d{6}$/,
            message: "请输入正确的验证码",
            trigger: "onChange",
          },
        ],
      },
    };
  },
  methods: {
    ...mapActions(["asyncSetToken"]),
    onSubmit() {
      this.isloading = true;
      try {
        // const resp = await longinAPI({
        // tel: this.fromLogin.mobile,
        // pwd: this.fromLogin.code,
        this.asyncSetToken({
          tel: this.fromLogin.mobile,
          pwd: this.fromLogin.code,
        });

        this.$router.replace({ path: this.$route.query.path || "/" });
        Notify({ type: "success", message: "登录成功" });

        // 跳转页面
        // this.$router.replace({ path: "/" });
        //优化
      } catch (error) {
        console.log(error);
        Notify({ type: "danger", message: "请求失败" });
      } finally {
        this.isloading = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.logo {
  width: 100px;
  height: 30px;
}
// .van-nav-bar {
//   background: #007bff;
//   /deep/ .van-nav-bar__title {
//     color: #fff;
//   }
// }
</style>
